<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus" 
          size="mini"
          @click="showOpen" 
        >NG 返工</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download" 
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:result:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="resultList" @selection-change="handleSelectionChange">
      <el-table-column label="标识" align="center" prop="id"  width="100"/>
      <el-table-column label="站位编码" align="center" prop="stationCode" width="80">
        <template slot-scope="scope">
          <span>OP{{ scope.row.stationCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品编码" align="center" prop="partNumber" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="生产结果" align="center" prop="testResult" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.node_test_result" :value="scope.row.testResult"/>
        </template>
      </el-table-column>
      
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.node_result_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="生成时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
 <!-- 添加或修改生产记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="重做开始站位">
          <el-select v-model="form.id" placeholder="请选择重新要做的站位">
            <el-option 
              v-for="item in resultList" 
              :key="item.id" 
              :label="'OP' + item.stationCode" 
              :value="item.id"  
              :disabled="item.status ==='9'">
            </el-option>
          </el-select> 
        </el-form-item>
      </el-form>
    <div class="el-form-item-tip" style="color: #E6A23C;">选择某个站点重做后，后续站点也需要重新做件！</div>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="reDoProduct">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResult, getResult, delResult, addResult, updateResult,delFlowResult } from "@/api/order/result";

export default {
  name: "Result",
  dicts: ['node_test_result','node_result_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 节点生产结果表格数据
      resultList: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层-操作数据
      operateTitle: "",
      detailOpen: false,
      // 当前生产详情数据
      currentTestDetail: null,
      // 生产到的数据类型
      detectedDataType: 'auto',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        nodeId: null,
        sortIndex: null,
        stationCode: null,
        ip: null,
        orderId: null,
        productId: null,
        partNumber: null,
        orderNo: null,
        testDetail: null,
        testResult: null,
        status: null,
        creator: null,
        updater: null,
        deleted: null,
        machineModel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    const partNumber = this.$route.params && this.$route.params.partId;
    this.queryParams.partNumber = partNumber;
    this.getList();
  },
  methods: {
    /** 查询节点生产结果列表 */
    getList() {
      this.loading = true;
      listResult(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.resultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    }, 
    showOpen() { 
      this.reset();
      this.open = true;
      this.title = "重做站点生产"; 
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nodeId: null,
        sortIndex: null,
        stationCode: null,
        ip: null,
        orderId: null,
        productId: null,
        partNumber: null,
        orderNo: null,
        testDetail: null,
        status: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        deleted: null,
        machineModel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加节点生产结果";
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认作废生产结果编号为"' + ids + '"以及后续站点的数据项？').then(function() {
        return delFlowResult(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("作废成功，可以从此站继续生产！");
      }).catch(() => {});
    },

    /** 重做按钮操作 */
    reDoProduct() {
      const selectedResult = this.resultList.find(item => item.id === this.form.id);
      const stationLabel = selectedResult ? `OP${selectedResult.stationCode}` : '';
      
      this.$modal.confirm(`确认重做站点从${stationLabel}开始以及后续的站位生产？`).then(() => {
        return delFlowResult(this.form.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("重做成功，可以从此站继续生产！");
        this.open = false;
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('order/result/export', {
        ...this.queryParams
      }, `result_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
